<template>
	<el-dialog title="物资申请对话框" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="80px" label-position="left">
			<el-form-item label="申请标题" prop="title">
				<el-input v-model="form.title" clearable placeholder="申请标题" maxlength="80"></el-input>
			</el-form-item>
			<el-form-item label="物资名称">{{suppliesName}}</el-form-item>
			<el-form-item label="可用数量">{{quantity}}</el-form-item>
			<el-form-item label="申请数量" prop="quantity">
				<el-input-number v-model="form.quantity" placeholder="申请数量" clearable></el-input-number>
			</el-form-item>
			<el-form-item label="使用人" prop="usedUser">
				<select-user-dialog v-model:accountId="form.usedUser" :mode="false"/>
			</el-form-item>
			<el-form-item label="备注说明">
				<el-input v-model="form.remark" type="textarea" placeholder="备注说明" maxlength="180" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import selectUserDialog from "@/views/module/selectUserDialog";
export default {
	components: {
		selectUserDialog,
	},
	emits: ['success', 'closed'],
	data() {
		return {
			visible: false,
			isSaveing: false,
			quantity:0,
			suppliesName:"",
			form: {
				suppliesId:'',
				title:"",
				quantity:0,
				usedUser: "",
				remark:""
			},
			rules: {
				title: [
					{required: true, message: '申请标题不能为空！'}
				],
				quantity: [
					{required: true, message: '申请数量不能为空！'}
				]
			},
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(){
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit(){
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					if (this.form.quantity > this.quantity) {
						this.$alert("申请数量不能超过库存数量", "提示", {type: 'error'})
					}else
					{
						this.isSaveing = true;
						var res = await this.$API.office.officeSupplies.insertOfficeSuppliesApply.post(this.form);
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success')
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				}
			})
		},
		//表单注入数据
		setData(data){
			this.quantity = data.quantity
			this.suppliesName = data.suppliesName
			this.form.suppliesId = data.suppliesId
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style>
</style>
